package com.service.impl;

import com.bean.Hw;
import com.bean.HwStu;
import com.dao.HwAttachDao;
import com.dao.HwDao;
import com.dao.HwStuDao;
import com.dao.impl.HwAttachDaoImpl;
import com.dao.impl.HwDaoImpl;
import com.dao.impl.HwStuDaoImpl;
import com.service.HwAttachService;
import com.service.HwService;
import com.utils.Page;

import java.util.List;

public class HwServiceImpl implements HwService {
    HwDao hwDao = new HwDaoImpl();
    HwAttachDao hwAttachDao = new HwAttachDaoImpl();
    HwStuDao hwApproved = new HwStuDaoImpl();

    @Override
    public Page<Hw> page(int pageNo, int pageSize, String content) {
        Page<Hw> page = new Page<>();
        page.setPageSize(pageSize);
        Integer pageTotalCount = hwDao.selectForPageTotalCount(content);
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        page.setPageTotal(pageTotal);
        page.setPageNo(pageNo);
        int begin = (page.getPageNo() - 1) * pageSize;
        List<Hw> items = hwDao.selectForPageItems(begin, pageSize, content);
        // 设置当前页数据
        page.setItems(items);
        return page;
    }

    @Override
    public Hw selectBy(Hw record) {
        return hwDao.selectBy(record);
    }

    @Override
    public int deleteById(Integer id) {
        int status = 0;
        hwAttachDao.deleteByHwId(id);
        if (hwDao.deleteByPrimaryKey(id) == -1) {
            status = 1;
        }
        return status;
    }

    @Override
    public int updeteInfo(Hw record) {
        int status = 0;
        if (hwDao.updateByPrimaryKey(record) == -1) {
            status = 1;
        }
        return status;
    }

    @Override
    public int insert(Hw record) {
        int status = 0;
        if (hwDao.insert(record) == -1) {
            status = 1;
        }
        return status;
    }

    @Override
    public Page<Hw> hwNoSubmit(Integer id, int pageNo, int pageSize) {
        if (id == null) {
            return null;
        }
        Page<Hw> page = new Page<>();
        page.setPageSize(pageSize);
        Integer pageTotalCount = hwDao.hwNoSubmitCount(id);
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        page.setPageTotal(pageTotal);
        page.setPageNo(pageNo);
        int begin = (page.getPageNo() - 1) * pageSize;
        List<Hw> items = hwDao.hwNoSubmit(id, begin, pageSize);
        // 设置当前页数据
        page.setItems(items);
        return page;
    }

    @Override
    public Page<Hw> hwSubmit(Integer id, int pageNo, int pageSize) {
        if (id == null) {
            return null;
        }
        Page<Hw> page = new Page<>();
        page.setPageSize(pageSize);
        Integer pageTotalCount = hwDao.hwSubmitCount(id);
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        page.setPageTotal(pageTotal);
        page.setPageNo(pageNo);
        int begin = (page.getPageNo() - 1) * pageSize;
        List<Hw> items = hwDao.hwSubmit(id, begin, pageSize);
        // 设置当前页数据
        page.setItems(items);
        return page;
    }

    @Override
    public Page<Hw> hwOverdue(Integer id, int pageNo, int pageSize) {
        if (id == null) {
            return null;
        }
        Page<Hw> page = new Page<>();
        page.setPageSize(pageSize);
        Integer pageTotalCount = hwDao.hwOverdueCount(id);
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        page.setPageTotal(pageTotal);
        page.setPageNo(pageNo);
        int begin = (page.getPageNo() - 1) * pageSize;
        List<Hw> items = hwDao.hwOverdue(id, begin, pageSize);
        // 设置当前页数据
        page.setItems(items);
        return page;
    }

    @Override
    public Page<Hw> hwOverTime(Integer id, int pageNo, int pageSize) {
        if (id == null) {
            return null;
        }
        Page<Hw> page = new Page<>();
        page.setPageSize(pageSize);
        Integer pageTotalCount = hwDao.hwOverTimeCount(id);
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        page.setPageTotal(pageTotal);
        page.setPageNo(pageNo);
        int begin = (page.getPageNo() - 1) * pageSize;
        List<Hw> items = hwDao.hwOverTime(id, begin, pageSize);
        // 设置当前页数据
        page.setItems(items);
        return page;
    }
    @Override
    public Page<HwStu> notComment(Integer id, int pageNo, int pageSize) {
        if (id == null) {
            return null;
        }
        Page<HwStu> page = new Page<>();
        page.setPageSize(pageSize);
        Integer pageTotalCount = hwDao.notCommentCount(id);
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        page.setPageTotal(pageTotal);
        page.setPageNo(pageNo);
        int begin = (page.getPageNo() - 1) * pageSize;
        List<HwStu> items = hwDao.notComment(id, begin, pageSize);
        // 设置当前页数据
        page.setItems(items);
        return page;
    }

    @Override
    public Page<HwStu> openComment(Integer id, int pageNo, int pageSize) {
        if (id == null) {
            return null;
        }
        Page<HwStu> page = new Page<>();
        page.setPageSize(pageSize);
        Integer pageTotalCount = hwDao.openCommentCount(id);
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        page.setPageTotal(pageTotal);
        page.setPageNo(pageNo);
        int begin = (page.getPageNo() - 1) * pageSize;
        List<HwStu> items = hwDao.openComment(id, begin, pageSize);
        // 设置当前页数据
        page.setItems(items);
        return page;
    }

    @Override
    public Page<Hw> myPage(Integer id, int pageNo, int pageSize) {
        if (id == null) {
            return null;
        }
        Page<Hw> page = new Page<>();
        page.setPageSize(pageSize);
        Integer pageTotalCount = hwDao.myPageCount(id);
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        page.setPageTotal(pageTotal);
        page.setPageNo(pageNo);
        int begin = (page.getPageNo() - 1) * pageSize;
        List<Hw> items = hwDao.myPage(id, begin, pageSize);
        // 设置当前页数据
        page.setItems(items);
        return page;
    }
}
